﻿using SiteUnity.Frame.Common.UITools.Interface;

namespace SiteUnity.Frame.Common.UITools
{
    public class TextBox : System.Web.UI.WebControls.TextBox, ITitleable, IRequireable, IValidateable
    {
        public TextBox() { }

        public bool isRequired { get; set; }
        public bool isValid {
            get
            {
                if (!isRequired)
                    return true;
                else if (string.IsNullOrEmpty(Text))
                    return false;
             
                return true;
            }
        }
        public bool ShowTitle { get; set; }
        public string Title { get; set; }
        public override string Text
        {
            get
            {
                return string.IsNullOrEmpty(base.Text) ? UITools.GetFormValue(this) : base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
        
        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);

            PrepareControl();
        }

        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);

            Page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page), "readyHINT", "$(document).ready(function () {$('textarea.HINT, input.HINT').hint();});",true);
        }

        private void PrepareControl()
        {
            if (ShowTitle)
            {
                this.CssClass += " HINT ";
                if (string.IsNullOrEmpty(Title))
                {
                    Title = this.ID;
                    this.Attributes.Add("Title", this.ID);
                }
                else
                    this.Attributes.Add("Title", Title);
            }
        }
    }
}
